/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.schnipsel.swt.editor.basis;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontData;

import de.mnit.basis.daten.FilterText;
import de.mnit.basis.daten.FormatText;
import de.mnit.basis.daten.WandleFolge;
import de.mnit.basis.daten.WandleText;
import de.mnit.basis.daten.WandleZahl;
import de.mnit.basis.daten.struktur.gruppe.Gruppe2;
import de.mnit.basis.daten.struktur.liste.Liste;
import de.mnit.basis.daten.struktur.liste.S_Liste;
import de.mnit.basis.daten.struktur.tabelle.typ.TypTabelle2;
import de.mnit.basis.sys.Ausgabe;

/**
 * @author Michael Nitsche
 * 26.06.2010	Erstellt
 */
public class KonverterHtml {

	/**
	 * TODO <font>
	 */
	public static Gruppe2<String,StyleRange[]> vonHTML(String text) {
		Ausgabe.debug("Setze HTML:",text);
		StringBuilder sb=new StringBuilder();
		text=text.replaceAll("<br>", "\n");

		boolean tag_b=false; //bild
		boolean tag_i=false; //kursiv
		boolean tag_s=false; //stroke
		boolean tag_u=false; //underline
		StyleRange offen=new StyleRange(0, 1, null, null);
		S_Liste<StyleRange> ranges=Liste.neu();

		String[] trenner=new String[]{"<b>", "</b>", "<i>", "</i>", "<u>", "</u>", "<strike>", "</strike>", "<br>"}; //, "<font .*>", "</font>"
		TypTabelle2<String, Boolean> tab=WandleText.inTeile(text, trenner);
		for(Gruppe2<String,Boolean> g : tab) {
			if(!g.g2()) { //Normaler Text
				String s=FormatText.vonHTMLZeichen(g.g1());
				sb.append(s);
				continue;
			}
			String s=g.g1();
			if(s.indexOf('/')>=0) {
				String tag=FilterText.trimme(s, "</>".toCharArray(), true, true);
				StyleRange sr=offen;
				sr.length = sb.length()-sr.start;
				iSRsetzeTag(sr, tag_b, tag_i, tag_u, tag_s);

				if(tag.equals("b")) tag_b=false;
				if(tag.equals("i")) tag_i=false;
				if(tag.equals("u")) tag_u=false;
				if(tag.equals("strike")) tag_s=false;
				offen=new StyleRange(sb.length(), 1, null, null); //+1???
				if(sr.length>0)
					ranges.plus(sr);
			}
			else {
				String tag=FilterText.trimme(s, "<>".toCharArray(), true, true);
				if(tag.equals("b")) tag_b=true;
				if(tag.equals("i")) tag_i=true;
				if(tag.equals("u")) tag_u=true;
				if(tag.equals("strike")) tag_s=true;
				offen=new StyleRange(sb.length(), 1, null, null);
			}
		}
		//offen abschliessen
		if(offen.start<sb.length()-1) {
			StyleRange sr=offen;
			iSRsetzeTag(sr, tag_b, tag_i, tag_u, tag_s);
			sr.length = sb.length()-sr.start;
			ranges.plus(sr);
		}
		return Gruppe2.neu(sb.toString(), (StyleRange[]) WandleFolge.zuArray(ranges));
	}

	public static String zuHTML(String s, StyleRange[] styles) {
		StringBuilder sb=new StringBuilder();
		int laenge=s.length();
		int naechstePos=0;
		for(StyleRange style : styles) {
			if(style.start>naechstePos) {
				String text=s.substring(naechstePos, style.start);
				text=FormatText.zuHTMLZeichen(text);
				sb.append(text);
			}
			sb.append(htmlFlags(style, true)); //Tag öffnen
			String text=s.substring(style.start, style.start+style.length); //-1?
			text=FormatText.zuHTMLZeichen(text);
			sb.append(text);
			sb.append(htmlFlags(style, false)); //Tag schließen
			naechstePos=style.start+style.length;
		}
		if(naechstePos<laenge) {
			String text=s.substring(naechstePos, laenge);
			text=FormatText.zuHTMLZeichen(text);
			sb.append(text);
		}
		String erg=sb.toString();
		erg=erg.replaceAll("\n", "<br>");
		Ausgabe.debug("HTML generiert:",erg);
		return erg;
	}

	private static String htmlFlags(StyleRange sr, boolean oeffnen) {
		String erg="";
		if((sr.fontStyle & SWT.BOLD) == SWT.BOLD)
			erg+= oeffnen ? "<b>" : "</b>";
		if((sr.fontStyle & SWT.ITALIC) == SWT.ITALIC)
			erg+= oeffnen ? "<i>" : "</i>";
		if(sr.underline) //TODO Doppelt unterstrichen???
			erg+= oeffnen ? "<u>" : "</u>";
		if(sr.strikeout)
			erg+= oeffnen ? "<strike>" : "</strike>";
		if(sr.foreground!=null)
			erg+= oeffnen ? "<font color="+htmlFarbe(sr.foreground)+">" : "</font>";
		if(sr.font!=null && sr.font.getFontData().length>0) {
			FontData data=sr.font.getFontData()[0];
			String style="";
			if(data.name!=null && data.name.length()>0)
				style+="font-family:"+data.name+";";
			if((data.style & SWT.ITALIC) == SWT.ITALIC)
				style+="font-style:italic;";
			if((data.style & SWT.BOLD) == SWT.BOLD)
				style+="font-weight:bold;";
			style+="font-size:x-large;"; //data.height

			erg+= oeffnen ? "<font style=\""+style+"\">" : "</font>";
		}
		return erg;
	}

	private static String htmlFarbe(Color c) {
		return "#"+WandleZahl.zuHex(new byte[]{(byte)c.getRed(), (byte)c.getGreen(), (byte)c.getBlue()});
	}

	private static void iSRsetzeTag(StyleRange sr, boolean b, boolean i, boolean u, boolean s) {
		if(b) sr.fontStyle = sr.fontStyle | SWT.BOLD;
		if(u) sr.underline=true;
		if(i) sr.fontStyle = sr.fontStyle | SWT.ITALIC;
		if(s) sr.strikeout=true;
	}

}
